package cn.shop.front.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.shop.front.service.CartService;
import cn.shop.transcation.order.entity.Cart;
import cn.shop.transcation.order.entity.CertQueryReq;
import cn.shop.transcation.order.entity.Order;
import cn.shop.transcation.order.mapper.CartMapper;
import cn.shop.transcation.order.mapper.OrderMapper;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import cn.shop.utils.enums.OrderStatus;
import cn.shop.utils.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2022/5/23 10:49
 * @description :
 */
@Slf4j
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<Long> cartOrders(long uid, String alias) {
        return baseMapper.cartOrders(uid, alias);
    }

    @Override
    public Map<String, Object> carts(CertQueryReq req, String alias) {
        Page<Cart> page = baseMapper.queryCarts(Page.of(req.getCurrent(), req.getSize()), req, alias);

        return PageUtil.formatPage(page, this::format);
    }

    @Override
    public boolean saveCarts(List<Long> ids, long uid, String alias) {
        LambdaQueryWrapper<Order> query = Wrappers.<Order>lambdaQuery()
                .in(Order::getId, ids)
                .eq(Order::getStatus, OrderStatus.PASSED);
        List<Order> orders = orderMapper.selectList(query);
        if (orders.isEmpty()) {
            log.info("订单号 {} 经过滤不满足保存到购物车的条件", ids);
            throw new ServiceException("所选订单已经全部售出或下架，请重新选择");
        }
        List<Cart> carts = orders.stream().map(o -> format(o, uid)).collect(Collectors.toList());
        return saveBatch(carts);
    }

    @Override
    public boolean deleteCarts(List<Long> ids) {
        return removeByIds(ids);
    }

    private Cart format(Order order, long uid) {
        Cart cart = new Cart();
        cart.setAmount(order.getAmount());
        cart.setGoods(order.getGoods());
        cart.setGoodsNum(order.getGoodsNum());
        cart.setOrderId(order.getId());
        cart.setPartitionId(order.getPartitionId());
        cart.setUserId(uid);
        return cart;
    }

    private Map<String, Object> format(Cart cart) {
        return Putif.start()
                .anyway("id", cart.getId())
                .anyway("alias", cart.getAlias())
                .anyway("amount", cart.getAmount())
                .anyway("game", cart.getGame())
                .anyway("goods", cart.getGoods())
                .anyway("goodsNum", cart.getGoodsNum())
                .anyway("orderId", cart.getOrderId())
                .anyway("partition", cart.getPartitionName())
                .anyway("partitionId", cart.getPartitionId())
                .anyway("time", cart.getCreateTime())
                .anyway("status", cart.getStatus())
                .get();
    }
}
